﻿using DL.Initializers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DL.Repositories
{
    public class NguoiDungRepository
    {
        private SqlConnection _connect;
        private SqlDataAdapter _sqlAdapter;

        public string CapNhatNguoiDung(long IdNguoiDung, string strEmail, string strPass, string strDienThoai, string strDiaChi, string strHoTen)
        {
            try
            {
                if (IdNguoiDung == 0)
                {
                    ConnectDataBase.CloseConnection(_connect);
                    _connect = new SqlConnection();
                    _connect = ConnectDataBase.GetSQlConnection(_connect);
                    var cm = _connect.CreateCommand();
                    cm.CommandText = "dbo.C_INSERT_NGUOIDUNG";
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.Parameters.Add("@email", SqlDbType.NVarChar).Value = strEmail;
                    cm.Parameters.Add("@password", SqlDbType.NVarChar).Value = strPass;
                    cm.Parameters.Add("@dienthoai", SqlDbType.NVarChar).Value = strDienThoai;
                    cm.Parameters.Add("@diachi", SqlDbType.NVarChar).Value = strDiaChi;
                    cm.Parameters.Add("@hoten", SqlDbType.NVarChar).Value = strHoTen;
                    cm.Parameters.Add("@NewQuoteNumber", SqlDbType.Int).Direction = ParameterDirection.Output;
                    //cm.Parameters.Add(newReqNumber);
                    cm.ExecuteNonQuery();
                    string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                    if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();
                }
                else
                {
                    if (strPass != null || strHoTen != null)
                    {
                        //Update
                        ConnectDataBase.CloseConnection(_connect);
                        _connect = new SqlConnection();
                        _connect = ConnectDataBase.GetSQlConnection(_connect);
                        var cm = _connect.CreateCommand();
                        cm.CommandText = "dbo.C_UPDATE_NGUOIDUNG";
                        cm.CommandType = CommandType.StoredProcedure;
                        cm.Parameters.Add("@id", SqlDbType.BigInt).Value = IdNguoiDung;
                        cm.Parameters.Add("@matkhau", SqlDbType.NVarChar).Value = strPass;
                        cm.Parameters.Add("@dienthoai", SqlDbType.NVarChar).Value = strDienThoai;
                        cm.Parameters.Add("@diachi", SqlDbType.NVarChar).Value = strDiaChi;
                        cm.Parameters.Add("@hoten", SqlDbType.NVarChar).Value = strHoTen;
                        cm.Parameters.Add("@NewQuoteNumber", SqlDbType.Int).Direction = ParameterDirection.Output;
                        cm.ExecuteNonQuery();
                        string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                        if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();

                    }
                    else
                    {
                        //Xóa người dùng
                        ConnectDataBase.CloseConnection(_connect);
                        _connect = new SqlConnection();
                        _connect = ConnectDataBase.GetSQlConnection(_connect);
                        var cm = _connect.CreateCommand();
                        cm.CommandText = "dbo.C_DELETE_NGUOIDUNG";
                        cm.CommandType = CommandType.StoredProcedure;
                        cm.Parameters.Add("@id", SqlDbType.NVarChar).Value = IdNguoiDung;
                        cm.Parameters.Add("@NewQuoteNumber", SqlDbType.Int).Direction = ParameterDirection.Output;
                        //cm.Parameters.Add(newReqNumber);
                        cm.ExecuteNonQuery();
                        string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                        if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();
                    }

                }
                return "Cập nhật thành công";
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDataBase.CloseConnection(_connect);
            }
        }

        public DataTable Login(string strEmail, string strMatKhau)
        {
            try
            {
                ConnectDataBase.CloseConnection(_connect);
                _connect = new SqlConnection();
                _connect = ConnectDataBase.GetSQlConnection(_connect);
                var cm = _connect.CreateCommand();
                cm.CommandText = "dbo.C_DANHSACH_NGUOIDUNG";
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add("@email", SqlDbType.NVarChar).Value = strEmail;
                cm.Parameters.Add("@matkhau", SqlDbType.NVarChar).Value = strMatKhau;
                var tableGs = new DataTable();
                _sqlAdapter = new SqlDataAdapter(cm);
                _sqlAdapter.Fill(tableGs);
                return tableGs;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDataBase.CloseConnection(_connect);
            }
        }
        public DataTable DanhSachNguoiDung(int loai = 0)
        {
            try
            {
                ConnectDataBase.CloseConnection(_connect);
                _connect = new SqlConnection();
                _connect = ConnectDataBase.GetSQlConnection(_connect);
                var cm = _connect.CreateCommand();
                cm.CommandText = "dbo.C_DS_NGUOIDUNG";
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add("@loai", SqlDbType.Int).Value = loai;
                var tableGs = new DataTable();
                _sqlAdapter = new SqlDataAdapter(cm);
                _sqlAdapter.Fill(tableGs);
                return tableGs;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDataBase.CloseConnection(_connect);
            }
        }

        public DataTable CheckEmail(string strEmail)
        {
            try
            {
                ConnectDataBase.CloseConnection(_connect);
                _connect = new SqlConnection();
                _connect = ConnectDataBase.GetSQlConnection(_connect);
                var cm = _connect.CreateCommand();
                cm.CommandText = "dbo.C_CHECKEMAIL_NGUOIDUNG";
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add("@email", SqlDbType.NVarChar).Value = strEmail;
                var tableGs = new DataTable();
                _sqlAdapter = new SqlDataAdapter(cm);
                _sqlAdapter.Fill(tableGs);
                return tableGs;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDataBase.CloseConnection(_connect);
            }
        }
        public string DoiMatKhau(long IdNguoiDung, string matkhaucu, string matkhaumoi)
        {
            try
            {
                ConnectDataBase.CloseConnection(_connect);
                _connect = new SqlConnection();
                _connect = ConnectDataBase.GetSQlConnection(_connect);
                var cm = _connect.CreateCommand();
                cm.CommandText = "dbo.C_UPDATE_MATKHAU_NGUOIDUNG";
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add("@id", SqlDbType.BigInt).Value = IdNguoiDung;
                cm.Parameters.Add("@matkhau", SqlDbType.NVarChar).Value = matkhaucu;
                cm.Parameters.Add("@matkhaumoi", SqlDbType.NVarChar).Value = matkhaumoi;
                cm.Parameters.Add("@NewQuoteNumber", SqlDbType.Int).Direction = ParameterDirection.Output;
                cm.ExecuteNonQuery();
                string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();
                return "Thành công";
            }
            catch (Exception)
            {
                return "Thất bại";
            }
            finally
            {
                ConnectDataBase.CloseConnection(_connect);
            }
        }


    }
}

